package com.insist.controller;

import com.insist.entity.OrderInfo;
import com.insist.framework.CmmBaseResponse;
import com.insist.model.OrderDetailInfoModel;
import com.insist.service.OrderService;
import com.insist.util.CmmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2016/7/1.
 */
@Controller
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/createOrder")
    @ResponseBody
    public CmmBaseResponse createOrder(){
        try {
            return orderService.createOrder(CmmUtils.getLoginUserId());
        }catch (Exception e){
            return CmmBaseResponse.buildResponse(false,"订单创建失败",e.getMessage());
        }
    }

    @RequestMapping("/createOrderByProductId")
    @ResponseBody
    public CmmBaseResponse createOrderByProductId(Long productId,Integer number){
        try {
            return orderService.createOrder(CmmUtils.getLoginUserId(),productId,number);
        }catch (Exception e){
            return CmmBaseResponse.buildResponse(false,"订单创建失败",e.getMessage());
        }
    }
    @RequestMapping("/findOrder")
    @ResponseBody
    public List<OrderInfo> findOrder(){
        return orderService.findAllOrders(CmmUtils.getLoginUserId());
    }

    @RequestMapping("/getOrderDetails")
    @ResponseBody
    public List<OrderDetailInfoModel> getOrderDetails(){
        List<OrderDetailInfoModel> list =  orderService.findAllOrderDetails(CmmUtils.getLoginUserId());
        for(OrderDetailInfoModel model:list){
            model.setProductImage(CmmUtils.replacePictureUrl(model.getProductImage()));
        }
        return list ;
    }

    @RequestMapping("/getOrderDetailByOrderId")
    @ResponseBody
    public List<OrderDetailInfoModel> getOrderDetailByOrderId(Long orderId){
        List<OrderDetailInfoModel> list =  orderService.findAllOrderDetails(CmmUtils.getLoginUserId(),orderId);
        for(OrderDetailInfoModel model:list){
            model.setProductImage(CmmUtils.replacePictureUrl(model.getProductImage()));
        }
        return list ;
    }

}
